package com.mapull.list;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 删除 List 中的特性值
 * @author mapull
 */
public class RemoveValues {
    private static List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E", "F", "E", "D", "E"));

    public static void main(String[] args) {
//        removeOneElement(list);
//        removeElement(list);
        rmDup(list);
    }

    public static void removeOneElement(List<String> list){
        String removed = "D";
        list.remove(removed);
        System.out.println(list);
    }

    public static void removeElement(List<String> list){
        String removed = "D";
        for(String str : list){
            if(str.equals(removed)){
                list.remove(str);
            }
        }
    }

    public static void removeElementWhile(List<String> list){
        String removed = "D";
        while(list.contains(removed)){
            list.remove(removed);
        }
        System.out.println(list);
    }

    public static void removeElementIte(List<String> list){
        String removed = "D";
        for(Iterator<String> it = list.iterator(); it.hasNext();){
            String value = it.next();
            if(value.equals(removed)){
                it.remove();
            }
        }
        System.out.println(list);
    }

    public static void remainElement(List<String> list){
        String removed = "D";
        List<String> results = new ArrayList<>();
        for(String str : list){
            if(!str.equals(removed)){
                results.add(str);
            }
        }
        System.out.println(results);
    }

    public static void rmElementFilter(List<String> list){
        String removed = "D";
        List<String> results = list.stream().filter(s -> !s.equals(removed)).collect(Collectors.toList());
        System.out.println(results);
    }

    public static void rmElementRemoveIf(List<String> list){
        String removed = "D";
        list.removeIf( s -> s.equals(removed));
        System.out.println(list);
    }

    private static void rmDup(List<String> list){
        Set<String> set = new HashSet<>(list);
        List<String> strings = new ArrayList<>(set);
        System.out.println(strings);
    }

    private static void rmDupLambda(List<String> list){
        List<String> results = list.stream()
                .distinct()
                .collect(Collectors.toList());
    }
}
